package com.ai.dmc.index.web.action;

import com.ai.dmc.index.common.bean.StaffSessBean;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;

import net.sf.json.JSONObject;

/**
 */
public class LoginAction extends BaseAction {
	private static final long serialVersionUID = -3579032710235743396L;
	private static final Logger logger = LoggerFactory.getActionLog(LoginAction.class);

//	public String index() {
//		String method = "execute"; 
//		logger.debug(method, "user login.");
//		return SUCCESS;
//	}
	
	public String execute(){
		logger.info("login", "Start");
		JSONObject loginInfo = new JSONObject();
		InputObject inputObj = super.getInputObject();
		String userId = inputObj.getParams().get("userId");
		String userPassword = inputObj.getParams().get("userPassword");
		if ((null == userId || userId.trim().length() == 0)
				&& (null == userPassword
						|| userPassword.trim().length() == 0)) {
			loginInfo.put("loginCode", "1");
			loginInfo.put("loginDesc", "账号密码错误，请重新输入");
			super.sendJson(loginInfo.toString());
			return null;
		}
		
		OutputObject outPutObject = super.getOutputObject();
		StaffSessBean sessionBean = (StaffSessBean) outPutObject.getObject();
//		userPassword = CryptionUtil.encrypt(userPassword);
		if (null == sessionBean) {
			loginInfo.put("loginCode", "11");
			loginInfo.put("loginDesc", "用户不存在");
		} else if (!userPassword.equals(sessionBean.getUserPassword())) {
			loginInfo.put("loginCode", "1");
			loginInfo.put("loginDesc", "账号密码错误，请重新登录");
		} else if (!"1".equals(sessionBean.getStatus())) {
			loginInfo.put("loginCode", "2");
			loginInfo.put("loginDesc", "此账号已停用，请联系管理员");
		}

		// 验证失败
		if (loginInfo.has("loginCode")) {
			outPutObject.setObject(loginInfo);
			super.sendJson(super.convertOutputObject2Json(outPutObject));
			return null;
		}
		loginInfo.put("loginCode", "0");
		loginInfo.put("loginDesc", "登录成功");
		outPutObject.setObject(loginInfo);
		super.getSession().setAttribute("dtkl_session", sessionBean);
		super.sendJson(super.convertOutputObject2Json(outPutObject));
		logger.info("login", "End");
		return null;
	}
}
